//
//  JSMLeavingWithCollectionTableViewCell.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMLeavingWithCollectionTableViewCell.h"
#import "JSMLeavingCollectionViewCell.h"
#import "JSMLeavingListModel.h"
#import "JSMLeavingMainAndHotModel.h"


@interface JSMLeavingWithCollectionTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, strong) UICollectionViewFlowLayout *leavingFlowLayout;
@property(nonatomic, strong) BaseCollectionView *leavingCollectionView;
@property(nonatomic, strong) NSMutableArray *mArr;
@end
@implementation JSMLeavingWithCollectionTableViewCell
// 重写set方法
- (void)setMArr:(NSMutableArray *)mArr {
    _mArr = mArr;
    [self.leavingCollectionView reloadData];
}

- (void)setMainMarr:(NSMutableArray *)mainMarr {
    _mainMarr = mainMarr;
    [self createData];
    [self.leavingCollectionView reloadData];
}

- (void)createData {
    self.mArr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.mainMarr.count; i++) {
        if (i < 3) {
            [self.mArr addObject:self.mainMarr[i]];
        }
    }
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
}
    return self;
}

#pragma mark -
- (BaseCollectionView *)leavingCollectionView {
    if (!_leavingCollectionView) {
        self.leavingCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:self.leavingFlowLayout];
    }

    _leavingCollectionView.dataSource = self;
    _leavingCollectionView.delegate = self;
    _leavingCollectionView.showsVerticalScrollIndicator = NO;
    _leavingCollectionView.scrollEnabled = NO;
    [_leavingCollectionView registerClass:[JSMLeavingCollectionViewCell class] forCellWithReuseIdentifier:@"leavingCollectionViewCell"];
    return _leavingCollectionView;
}

- (void)createView {
    self.leavingFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    [self.contentView addSubview:self.leavingCollectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSMLeavingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"leavingCollectionViewCell" forIndexPath:indexPath];
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[[self.mainMarr[indexPath.row] dataArr][0] index_cover]] placeholderImage:PlaceHolderImage];
    cell.descriptLabel.text = [[self.mainMarr[indexPath.row] dataArr][0] textStr];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate pushToNextPage:indexPath.item];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.leavingFlowLayout.itemSize = CGSizeMake(TableViewCellWidth * 4 / 14, TableViewCellHeight * 7 / 8);
    self.leavingCollectionView.frame = CGRectMake(0, 0, TableViewCellWidth, TableViewCellHeight);
    self.leavingCollectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.leavingFlowLayout.footerReferenceSize = CGSizeMake(TableViewCellWidth, 30);
}

@end
